<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //json: 是一种数据格式,是字符串类型,主流的数据传输格式
      //有固定格式的字符串 ,长得有点像对象
      var str = `{"name":"zhangsan","age":20,"height":"170cm"}`;
      console.log(str);

      var str2 = `[{"name":"zhangsan",{"age":20}},{"height":"170cm"}]`;

      //js语言: var arr = ["张三", "李四"];
      //php语言 $arr = array(0=>"张三",1=>"李四")

      //js数据----转成json数据---传给其他语言---->把json数据转成自己可以识别的语言

      //json的转换
      //1.把js里的数组或者对象转成json字符串: JSON.stringify();
      var obj = { name: "张三", age: 20 };
      // var obj = [
      //   { name: "张三", age: 20 },
      //   { name: "张三", age: 20 },
      //   { name: "张三", age: 20 },
      // ];
      // var obj = ["张三", "李四", "王五"];
      console.log(obj);
      var str = JSON.stringify(obj);
      console.log(str);

      //把json数据转成数组或者对象  JSON.parse
      //严格意义上json里是双引号
      var str = '{"name":"张三","age":20,"height":"170cm"}';
      var obj = JSON.parse(str);
      console.log(obj);

      //3.json深拷贝
      var obj1 = {
        name: "张三",
        age: 20,
      };
      console.log(obj1);
      var obj2 = JSON.parse(JSON.stringify(obj1)); //复制过来
      console.log(obj2);
      obj2.name = "李四";
      console.log(obj1);
      console.log(obj2);

      //4.json深拷贝 会忽略一些类型
      //json深拷贝会忽略  函数 undefined  symbol  map
      var obj = {
        name: "张三",
        fn: function () {
          console.log(fn);
        },
        a: undefined,
        b: null,
        boo: true,
        num: 123,
      };
      console.log(obj);
      var obj2 = JSON.parse(JSON.stringify(obj));
      console.log(obj2); //深拷贝后掉了 函数 undefined
    </script>
  </body>
</html>
